package com.tlsc;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.*;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.AttachmentRenderPolicy;
import com.tlsc.model.TestAModel;
import com.tlsc.model.TestBModel;
import com.tlsc.model.TestCModel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @note:
 * @author: HuangXueJi
 * @date: 2025/5/9
 **/
public class Main {
    public static void main(String[] args) {
//总参考 https://deepoove.com/poi-tl/#plugin-attachment

        System.out.println("Hello world!");
        Map<String, Object> data = new HashMap<>();
        data.put("name", "Huang You");
        data.put("age", 18);
        data.put("birthday", "2025-01-01");

        String picUrl = "http://192.168.1.140:8066/tly-java/UploadFiles/20250327/7ec56778-877d-4b96-af3f-9a2f41ffd84c/.jpg/caf41cc3-87b0-4f46-9a77-8a43c7d6b15f.jpg_100x100.jpg";
        List<Map<String, Object>> testBList = new ArrayList<>();
        Map<String, Object> testBModel = new HashMap<>();
        testBModel.put("name", "TestBName  1111");
        List<Map<String, Object>> testCList = new ArrayList<>();
        Map<String, Object> testCModel = new HashMap<>();
        testCModel.put("name", "TestCName  1111");
        testCModel.put("picUrl", Pictures.ofUrl(picUrl).create());//图片
        testCModel.put("url", Texts.of("地址").link("http://192.168.1.100").create());//超链接
        testCList.add(testCModel);

        Map<String, Object> testCModel2 = new HashMap<>();
        testCModel2.put("name", "TestCName  2222");
        testCModel2.put("picUrl", Pictures.ofUrl("http://192.168.1.140:8066/tly-java/UploadFiles/20250428/fd6960bc-2a71-11ed-a5aa-000c297622a4/.jpg/aa8de260-1fa5-49a3-a8a7-5000e7c3d178.jpg").create());
        testCModel2.put("url", Texts.of("地址2").link("http://192.168.1.100").create());
        testCList.add(testCModel2);
        testBModel.put("testCList", testCList);
        testBList.add(testBModel);
        data.put("testBList", testBList);
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        AttachmentRenderData attach = Attachments.ofLocal("D:\\javaCode\\java-poi-tl-demo\\source\\poi-tl-word\\src\\main\\resources\\test.docx", AttachmentType.DOCX).create();
        Configure config = Configure.builder()
                .bind("testBList", policy)//用于表格循环
                .bind("attachment", new AttachmentRenderPolicy())//用于附件
                .build();
        data.put("attachment", attach);

        XWPFTemplate template = XWPFTemplate.compile("D:\\javaCode\\java-poi-tl-demo\\source\\poi-tl-word\\src\\main\\resources\\test.docx", config)
                .render(data);

        try {
            template.writeAndClose(new FileOutputStream("output.docx"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}